<template>
    <div class="layout">
        <div v-if="login"><!--默认login为false，所以一开始显示的事登陆界面-->
            <Layout :style="{minHeight: '100vh'}">
            <Sider collapsible :collapsed-width="78" v-model="isCollapsed">
                <Menu active-name="1-2" theme="dark" width="auto" :class="menuitemClasses">
                    <router-link to="/user"><!--这个标签是路由的链接标签-->
                        <MenuItem name="1-1">
                            <Icon type="person"></Icon>
                            <span>用户管理</span>
                        </MenuItem>
                    </router-link>
                    <router-link to="/cate">
                        <MenuItem name="1-2">
                            <Icon type="navicon-round"></Icon>
                            <span>分类管理</span>
                        </MenuItem>
                    </router-link>
                    <router-link to="/product">
                        <MenuItem name="1-3">
                            <Icon type="settings"></Icon>
                            <span>产品管理</span>
                        </MenuItem>
                    </router-link>
                    <router-link to="/news">
                        <MenuItem name="1-4">
                            <Icon type="heart"></Icon>
                            <span>新闻管理</span>
                        </MenuItem>
                    </router-link>
                    <router-link to="/album">
                        <MenuItem name="1-5">
                            <Icon type="camera"></Icon>
                            <span>相册管理</span>
                        </MenuItem>
                    </router-link>
                </Menu>
            </Sider>
            <Layout>
                <Header :style="{background: '#fff', boxShadow: '0 2px 3px 2px rgba(0,0,0,.1)'}"></Header>
                <Content :style="{padding: '0 16px 16px'}">
                    <Breadcrumb :style="{margin: '16px 0'}">
                        <BreadcrumbItem>Home</BreadcrumbItem>
                        <BreadcrumbItem>Components</BreadcrumbItem>
                        <BreadcrumbItem>Layout</BreadcrumbItem>
                    </Breadcrumb>
                    <Card>
                        <div style="height: 600px">
                            <router-view></router-view><!--路由中引用相应模块的内容-->
                        </div>
                    </Card>
                </Content>
            </Layout>
        </Layout>
        </div>
        <div v-else style="margin:60px 60px">
            <h3 style="margin-bottom:30px;color:#ccc;font-size:22px;font-weight:500">请登陆</h3>
            <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
                <FormItem prop="user">
                    <Input type="text" v-model="formInline.user" placeholder="Username">
                        <Icon type="ios-person-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <br/>
                <FormItem prop="password">
                    <Input type="password" v-model="formInline.password" placeholder="Password">
                        <Icon type="ios-locked-outline" slot="prepend"></Icon>
                    </Input>
                </FormItem>
                <br/>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('formInline')">Signin</Button>
                </FormItem>
            </Form>
        </div>

        
    </div>
</template>
<script>
import {mapGetters} from 'vuex';
import {mapActions} from 'vuex';
    export default {
        data () {
            return {
                isCollapsed: false,

                formInline: {
                    user: '',
                    password: ''
                },
                ruleInline: {
                    user: [
                        { required: true, message: 'Please fill in the user name', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: 'Please fill in the password.', trigger: 'blur' },
                        { type: 'string', min: 6, message: 'The password length cannot be less than 6 bits', trigger: 'blur' }
                    ]
                }
            };
        },
        computed: {
            ...mapGetters(['login']),
            menuitemClasses: function () {
                return [
                    'menu-item',
                    this.isCollapsed ? 'collapsed-menu' : ''
                ]
            }
        },
        methods:{
            ...mapActions(['checkLogin']),
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$Message.success('登陆成功!');
                        this.checkLogin(true);//此时通过传参将login改为true
                    } else {
                        this.$Message.error('用户名或密码错误!');
                    }
                });
            }
        }
    }
</script>
<style scoped>
    .layout-con{
        height: 100%;
        width: 100%;
    }
    .menu-item span{
        display: inline-block;
        overflow: hidden;
        width: 69px;
        text-overflow: ellipsis;
        white-space: nowrap;
        vertical-align: bottom;
        transition: width .2s ease .2s;
    }
    .menu-item i{
        transform: translateX(0px);
        transition: font-size .2s ease, transform .2s ease;
        vertical-align: middle;
        font-size: 16px;
    }
    .collapsed-menu span{
        width: 0px;
        transition: width .2s ease;
    }
    .collapsed-menu i{
        transform: translateX(5px);
        transition: font-size .2s ease .2s, transform .2s ease .2s;
        vertical-align: middle;
        font-size: 22px;
    }
</style>